package com.tbit.main.controller;


import com.tbit.main.common.JsonResult;
import com.tbit.main.exception.BaseException;
import com.tbit.main.pojo.*;
import com.tbit.main.service.*;
import com.tbit.main.util.Assert;
import com.tbit.main.util.PhoneUtil;
import com.tbit.main.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 强制结束订单日志
 *
 * @author Leon
 * 2019年11月23日 上午10:18:31
 */
@RestController
@RequestMapping("/mainFinishOrderLog")
public class MainFinishOrderLogController {
    @Autowired
    private MainFinishOrderLogService mainFinishOrderLogService;
    @Autowired
    private MachineService machineService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private UserService userService;
    @Autowired
    private RideLogService rideLogService;
    @Autowired
    private AccountService accountService;

    /**
     * 查询统计手机号，设备号
     */
    @RequestMapping("/query")
    public JsonResult getByTimeAll(String token, Integer accountId, String startTime, String endTime,
                                   @RequestParam(value = "userCode", required = false) String userCode,
                                   @RequestParam(value = "codePhone", required = false) String codePhone) {
        AccountUser accountUserToken = tokenService.getAccount(token);

        Assert.hasLength(startTime, "System.startTime.notNull");
        Assert.hasLength(endTime, "System.endTime.notNull");
        Assert.notNull(accountId, "AccountUser.agentId.notNull");

        List<MainFinishOrderLog> mainFinishOrderLogList = new LinkedList<>();
        if (userCode == null && codePhone == null) {
            mainFinishOrderLogList = mainFinishOrderLogService.query(accountId, userCode, startTime, endTime);
        }
        if (userCode != null) {
            /**查询设备信息*/
            Machine machine = machineService.getByUseCode(userCode);
            if (machine == null) {
                throw new BaseException("Machine.notExist");
            }

            /**设备权限判断*/
            machineService.auth(accountUserToken.getAgents(), machine.getMachineId());

            mainFinishOrderLogList = mainFinishOrderLogService.queryAll(accountId, userCode, startTime, endTime, accountUserToken.getAccountUserId());
        }
        if (codePhone != null) {
            if (!PhoneUtil.isPhone(codePhone)) {
                /**查询设备信息*/
                Machine machine = machineService.getByUseCode(codePhone);
                if (machine == null) {
                    throw new BaseException("Machine.notExist");
                }

                /**设备权限判断*/
                machineService.auth(accountUserToken.getAgents(), machine.getMachineId());
                mainFinishOrderLogList = mainFinishOrderLogService.queryAll(accountId, machine.getUserCode(), startTime, endTime, accountUserToken.getAccountUserId());
            } else {
                Integer account = accountService.getByAccountAndType(accountUserToken.getAccountId(), 1).getAccountId();

                User user = userService.getByPhone(account, codePhone);
                if (user == null) {
                    throw new BaseException("AccountUser.notExist");
                }

                List<RideLog> rideLogList = rideLogService.getByMachineId(null, user.getUserId(), startTime, endTime);
                if (rideLogList.size() > 0) {
                    List<String> userCodes = rideLogList.stream().map(RideLog::getUserCode).collect(Collectors.toList());
                    mainFinishOrderLogList = mainFinishOrderLogService.queryAll(accountId, StringUtil.getStringListString(userCodes), startTime, endTime, accountUserToken.getAccountUserId());
                } else {
                    return JsonResult.succ();
                }
            }
        }
        return JsonResult.succ(mainFinishOrderLogList);
    }
}